﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Admin;
using System.Text;
using System.Windows.Browser;

namespace Common.Controls
{
    public partial class Login : UserControl
    {
        private Guid _studyId;
        public delegate void LoginCompletedDelegate(bool Success, int UserId);
        public event LoginCompletedDelegate LoginCompleted;
        public string Username { get; set; }
        public int UserId { get; set; }
        public Login(Guid StudyId)
        {
            //Used for both admin app and participant app
            //Except admin app will have blank Guid for study ID
            InitializeComponent();            
            _studyId = StudyId;
            //CreateAccount.Text = ConfigurationSettings.CreateUserAccountDisplay;
        }
        public void Focus()
        {
            tbUserName.Focus();
        }
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            btnLogin.IsEnabled = false;
            lblStatus.Text = "Validating your account ...";
            Error.Text = "";
            LoginUser();
        }
        private void LoginUser()
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.LoginUserCompleted += new EventHandler<LoginUserCompletedEventArgs>(delegate(object sender1, LoginUserCompletedEventArgs e1)
            {
                if (e1.Result)
                {
                    if (_studyId != Guid.Empty)
                    {
                        //Only validate claims if there is a study we are taking
                        lblStatus.Text = "Retrieving your information ...";
                        UserId = e1.UserId;
                        Username = tbUserName.Text;
                        //Validate the user has supplied all the necessary claims ...
                        AdminServiceWCFClient b = StudyManager.GetAdminServiceWCFClient();
                        b.ValidateUserClaimsCompleted += new EventHandler<ValidateUserClaimsCompletedEventArgs>(delegate(object sender2, ValidateUserClaimsCompletedEventArgs e2)
                        {
                            if (e2.Result)
                            {
                                LoginCompleted(true, e1.UserId);
                            }
                            else
                            {
                                ////Claims were not enough, so show what was missing.
                                //StringBuilder sb = new StringBuilder();
                                //sb.Append("Missing information required for this study. Please update your account and fill in these required fields: ");
                                //foreach (string ClaimName in e2.MissingClaimNames)
                                //{
                                //    sb.Append(ClaimName + ", ");
                                //}
                                //Error.Text = sb.ToString().Substring(0, sb.ToString().Length - 2);
                                //lblStatus.Text = "";
                                //btnLogin.IsEnabled = true;
                                ValidateClaims(tbUserName.Text,UserId);
                            }
                        });
                        b.ValidateUserClaimsAsync(_studyId, Username, UserId);
                    }
                    else
                    {
                        //No study, this is the admin program so just return user
                        LoginCompleted(true, e1.UserId);
                    }
                }
                else
                {
                    lblStatus.Text = "";
                    Error.Text = "Incorrect username or password.";
                    tbUserName.Text = "";
                    tbPassword.Password = "";
                    btnLogin.IsEnabled = true;
                }
            });
            a.LoginUserAsync(tbUserName.Text, tbPassword.Password);
        }

        void b_ValidateUserClaimsCompleted()
        {
            throw new NotImplementedException();
        }
        private void UserControl_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LoginUser();
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.Focus();
            tbUserName.Focus();
        }
        public delegate void ValidateClaimsDelegate(string LoginName, int UserId);
        public event ValidateClaimsDelegate ValidateClaims;
        public delegate void CreateAccountDelegate();
        public event CreateAccountDelegate CreateAccountEvent;
        private void CreateAccount_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            CreateAccountEvent();
        }
        private void btnCreateAccount_Click(object sender, RoutedEventArgs e)
        {
            CreateAccountEvent();
        }
    }
}
